<?php

namespace Common\Model;

/**
 * 商品管理类
 *
 * Class GoodsModel
 * @package Common\Model
 */
class ProjectModel extends CommonModel
{
    /**
     * 数据库模型
     * @var string
     */
    private $goodsModel = '';

    /**
     * 构造方法
     */
    public function __construct()
    {
        $this->goodsModel = M('goods');
    }

    /**
     * 商品列表
     *
     * @param bool $sale_flg
     * @return mixed
     */
    public function getGoodsList($sale_flg = false)
    {
        $field = 'mx_goods.goods_id,
                  mx_goods.cat_id,
                  mx_goods.goods_sn,
                  mx_goods.goods_name,
                  mx_goods.market_price,
                  mx_goods.sale_price,
                  mx_goods.goods_img,
                  mx_goods.sale_num,
                  mx_goods.shelves_time,
                  mx_goods.update_time,
                  sum(s.stock_num) AS stock_number,
                  mx_goods.goods_status';

        $where = array(
            'mx_goods.del_flg' => 0
        );
        if ($sale_flg) {
            $where['mx_goods.goods_status'] = array(2, 3, 'OR');
        }

        $data = $this->goodsModel->field($field)
                                 ->join('LEFT JOIN mx_goods_stock s ON s.goods_id = mx_goods.goods_id ')
                                 ->group('mx_goods.goods_id')
                                 ->where($where)
                                 ->select();

        // 显示处理
        foreach ($data as $key => $val) {
            $data[$key]['goods_status_show'] = C('mx_goods_status')[$val['goods_status']];
        }
        return $data;
    }

    /**
     * 商品信息
     *
     * @param $gid
     * @param bool $sale_flg
     * @return bool|mixed
     */
    public function getGoodsInfo($gid, $sale_flg = false)
    {
        if (!$gid) {
            return false;
        }
        $where = array(
            'goods_id' => $gid,
            'del_flg' => 0
        );
        if ($sale_flg) {
            $where['goods_status'] = array(2, 3, 'OR');
        }
        $info = $this->goodsModel->where($where)->find();

        if (!$info) {
            return false;
        }

        // 商品处理
        return $info;
    }

    /**
     * 商品详细
     *
     * @param $gid
     * @return bool|mixed
     */
    public function getGoodsDetail($gid)
    {
        if (!$gid) {
            return false;
        }
        $where = array(
            'goods_id' => $gid,
            'mx_goods.del_flg'  => 0
        );
        $field = 'mx_goods.goods_name,
        mx_goods.goods_id,
        mx_goods.goods_sn,
        mx_goods.cat_id,
        mx_goods.market_price,
        mx_goods.sale_price,
        mx_goods.goods_detail,
        mx_goods.goods_style,
        mx_goods.goods_img,
        mx_goods.goods_quality,
        mx_goods.goods_status,
        mx_goods.sale_num,
        mx_goods.market_time,
        mx_goods.shelves_time,
        mx_goods.update_time,
        mx_goods.operator_id,
        c.cat_name,
        mx_goods.del_flg';
        $info = $this->goodsModel->field($field)
                                  ->join('LEFT JOIN mx_category c on c.cat_id = mx_goods.cat_id')
                                  ->where($where)
                                  ->find();
        if (!$info) {
            return false;
        }
        // 轮播图
        $gallery_list = M('goods_gallery')->where(array('goods_id' => $info['goods_id']))->select();
        if ($gallery_list) {
            //这里不要用for， 有几张就显示几张
            foreach ($gallery_list as $key => $val) {
                if (!empty($val['gallery_url'])) {
                    $gallery_list[$key]['gallery_url'] = showImg($val['gallery_url']);
                } else {
                    $gallery_list[$key]['gallery_url'] = C('MX_NO_IMG');
                }
            }
            $info['gallery'] = $gallery_list;
        } else {
            for ($i = 0; $i < 4; $i++) {
                $no_gallery_list[$i]['gallery_url'] = C('MX_NO_IMG');
            }
            $info['gallery'] = $no_gallery_list;
        }

        // 颜色
        $info['color_list'] = $this->getColorNameByGoods($gid);
        
        // 显示处理
        $info['goods_img']    = showImg($info['goods_img']);
        $info['quality_show'] = C('mx_quality')[$info['goods_quality']];
        $info['style_show']   = C('mx_style')[$info['goods_style']];
        $info['goods_detail'] = htmlspecialchars_decode($info['goods_detail']);

        return $info;
    }

    /**
     * 修改产品状态
     *
     * @param $gid
     * @param $status
     * @return bool
     */
    public function changeGoodsStatus($gid, $status)
    {
        if (!$gid) {
            return false;
        }
        $data = array(
            'goods_status' => $status
        );
        if ($status == 2) {
            $data['shelves_time'] = time();
        } else {
            $data['last_update'] = time();
        }
        return $this->goodsModel->where(array('goods_id' => array('in', $gid)))->save($data);
    }

    /**
     * 删除商品
     *
     * @param $gid
     * @return bool
     */
    public function delGoods($gid)
    {
        if (!$gid) {
            return false;
        }
        return $this->goodsModel->where(array('goods_id' => $gid))->save(array('del_flg' => 1));
    }

    /**
     * 获取颜色信息
     *
     * @param $id
     * @return bool
     */
    public function getColorNameByGoods($id)
    {
        if (!$id) {
            return false;
        }
        $color_list = M('goods_color')->where(array('goods_id' => $id))->select();
        if ($color_list) {
            $color_name = array();
            foreach ($color_list as $key => $val) {
                $color_name[$key] = $val['color_name'];
            }
            $res = implode(',', $color_name);
            return $res;
        } else {
            return '-';
        }
    }
}